package com.tchintech.pay.view.activity;

import android.os.Bundle;
import android.view.View;

import com.tchintech.pay.R;
import com.tchintech.pay.delegate.PasswordManageDelegate;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.model.entity.ResultBean;
import com.tchintech.pay.net.HttpRequester;
import com.tchintech.pay.utils.AppConst;
import com.tchintech.pay.utils.RegexUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/8 0008.
 */

public class PasswordManageActivity  extends ActivityPresenter<PasswordManageDelegate> implements View.OnClickListener{
    private final String authCreat = "AuthCode/createAuthCode";
    private final String update_URL = "/Merchant/updatePassword";
    private Map<String,String> map;
    private String authId = "";
    private String  str_oldPassword,str_newPassword,str_confirmPassword,str_update_ed_verificationcode;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        super.initView();
        viewDelegate.getTexts(R.id.update_tv_phoneNumber).setText(AppConst.getUser().getPhone());;
        viewDelegate.setOnClickListener(this,R.id.update_bt_updatePassword, R.id.update_bt_send_verification_code);

    }

    @Override
    protected void initData() {
        super.initData();
    }


    private void setData(){
         str_oldPassword = viewDelegate.getString(R.id.update_ed_oldpassword);
         str_newPassword = viewDelegate.getString(R.id.update_ed_newpassword);
         str_confirmPassword = viewDelegate.getString(R.id.update_ed_confirmPassword);
         str_update_ed_verificationcode = viewDelegate.getString(R.id.update_ed_verificationcode);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.update_bt_updatePassword:
                // TODO 判断密码
                if ("".equals(str_oldPassword) || "".equals(str_newPassword) || "".equals(str_confirmPassword)) {
                    viewDelegate.setTexts("请检查密码");
                    return;
                }
                if (!RegexUtils.checkPassword(str_newPassword) ){
                    viewDelegate.setTexts("请重新输入密码");
                    return;
                }
                if (!RegexUtils.checkPassword(str_newPassword) || !str_newPassword.equals(str_confirmPassword)) {
                    viewDelegate.setTexts("密码不一致");
                    return;
                }
                if ("".equals(authId)) {
                    viewDelegate.setTexts("请先发送验证码");
                    return;
                }
                if ("".equals(str_update_ed_verificationcode)) {
                    viewDelegate.setTexts("请输入验证码");
                    return;
                }

                map = new HashMap<>();
                map.put("phoneNumber", AppConst.getUser().getPhone());
                map.put("password", str_oldPassword);
                map.put("newPassword", str_newPassword);
                map.put("confirmPassword", str_confirmPassword);
                map.put("verificationCode", str_update_ed_verificationcode);
                map.put("authId", authId);
                httpPost(update_URL,map, HttpCode.passwordcode,null);
                break;
            case R.id.update_bt_send_verification_code:
                map = new HashMap<>();
                map.put("phoneNumber", AppConst.getUser().getPhone());
                map.put("authType", "UPDATEPASSWORD");
                httpPost(authCreat,map, HttpCode.pssswordupdata,null);
                break;
        }
    }

     /* /网路请求界面配置
     *
     /* @param url  请求url
     * @param map  请求所需的数据
     * @param what 界面回传标识符
     */
    /*private void httpPost(String url, Map<String, String> map, int what,File file) {
        String path = HttpRequester.path(url, map);
        try {
            showProgressDialog("", "加载中，请稍后...");
            HttpRequester.post(this, what, path, map, this, file);
        } catch (Exception e) {
            hideProgressDialog();
            showMsg("网络异常");
        }
    }*/

    @Override
    public void httpCallback(String resultBean, int what) {
        super.httpCallback(resultBean, what);
        switch (what){
            case HttpCode.passwordcode:
                System.out.println("数据源"+resultBean);
                break;
            case HttpCode.pssswordupdata:
                System.out.println("数据源"+resultBean);
                break;
        }
    }
    @Override
    protected Class<PasswordManageDelegate> getDelegateClass() {
        return PasswordManageDelegate.class;
    }
}

